package com.rfid.base.base;

import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.rfid.base.R;


public abstract class BaseTitleActivity extends AppCompatActivity {

    View mBack;
    TextView mTitle;
    View mRightView;

    int mRightViewRes = 0;
    View.OnClickListener mRightViewListener;

    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("getTitle() = " + getTitle());
        int back = getResources().getIdentifier("back","id",null);
        int title = getResources().getIdentifier("title","id",null);
        int right_view = getResources().getIdentifier("right_view","id",null);
        if (back != 0) {
            mBack = findViewById(back);
        }
        if (title != 0) {
            mTitle = findViewById(title);
        }
        if (right_view != 0) {
            mRightView = findViewById(right_view);
        }

        if (mRightView != null) {
            if (mRightViewListener == null) {
                mRightView.setVisibility(View.GONE);
            } else {
                if (mRightViewRes != 0) {
                    mRightView.setBackgroundResource(mRightViewRes);
                }
                mRightView.setOnClickListener(mRightViewListener);
            }
        }
        if (mTitle != null) {
            mTitle.setText(getTitle());
        }
        if (mBack != null) {
            mBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        if (mTitle != null) {
            mTitle.setText(title);
        }
    }

    public void setRightView(int res, View.OnClickListener listener) {
        mRightViewRes = res;
        mRightViewListener = listener;
        if (mRightView != null) {
            if (listener != null) {
                mRightView.setVisibility(View.VISIBLE);
                if (res != 0) {
                    mRightView.setBackgroundResource(res);
                }
                mRightView.setOnClickListener(listener);
            } else {
                mRightView.setVisibility(View.GONE);
                mRightView.setOnClickListener(null);
            }
        }
    }

}
